package Exercise;

import java.util.ArrayList;

/**
 * Description：
 * Author: zhangc
 * Date：2017/8/1 14:38
 */
public class TwoNumSum {

    public static ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
        ArrayList<Integer> list = new ArrayList<>();
        if (array == null || array.length < 1 || sum < array[ 0 ])
            return list;
        for (int i = 0; i < array.length; ++i) {
            for (int j = array.length - 1; j >= i; --j) {
                if (array[ i ] + array[ j ] == sum) {
                    list.add(array[ i ]);
                    list.add(array[ j ]);
                    return list;
                } else if (array[ i ] + array[ j ] < sum)
                    break;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 4, 7, 11, 15};
        ArrayList<Integer> integers = FindNumbersWithSum(array, 15);
        System.out.println(integers);
    }

}
